"""
@version:python3
@author:大橘为重
@file:12-QLineEdit-案例2.py
@time:2022/2/27 19:35
"""
# 0、导入需要的包和模块
from PyQt5.Qt import *
import sys

class AccountTool:
    ACCOUNT_ERROR = 1
    PWD_ERROR = 2
    SUCCESS = 3
    @staticmethod
    def login_check(account,pwd):
        if (account != "sz"):
            return AccountTool.ACCOUNT_ERROR
        if (pwd != "123"):
            return AccountTool.PWD_ERROR
        else:
            return AccountTool.SUCCESS


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLineEdit-案例2")
        self.resize(500, 500)
        self.setMinimumSize(400,400)
        self.setup_ui()
    def setup_ui(self): # 添加子控件以及子控件的配置操作




        self.account_le = QLineEdit(self)
        self.account_le.setPlaceholderText("请输入账号") # 设置占位文本
        self.pwd_le = QLineEdit(self)
        self.pwd_le.setPlaceholderText("请输入密码")
        self.pwd_le.setEchoMode(QLineEdit.Password) # 设置密码以密文显示
        self.pwd_le.setClearButtonEnabled(True) # 密码框自动显示清空按钮
        self.login_btn = QPushButton(self)
        self.login_btn.setText("登   陆")

        # 添加自定义行为操作（明文和密文的切换）

        action = QAction(self.pwd_le)
        action.setIcon(QIcon("close.png"))

        def change():
            if self.pwd_le.echoMode() == QLineEdit.Normal:
                self.pwd_le.setEchoMode(QLineEdit.Password)
                action.setIcon(QIcon("close.png"))
            else:
                self.pwd_le.setEchoMode(QLineEdit.Normal)
                action.setIcon(QIcon("open.png"))

        action.triggered.connect(change)
        self.pwd_le.addAction(action,QLineEdit.TrailingPosition)
        # self.pwd_le.addAction(action,QLineEdit.LeadingPosition)


        # 设置文本自动补全
        completer = QCompleter(["sz","shunzi","wangzha"],self.account_le)
        self.account_le.setCompleter(completer)


        self.login_btn.pressed.connect(self.login)

    def login(self):
        account = self.account_le.text()
        pwd = self.pwd_le.text()

        status = AccountTool.login_check(account,pwd)

        if (status == AccountTool.ACCOUNT_ERROR):
            print("账号错误")
            self.account_le.clear()
            # self.account_le.setText("")
            self.pwd_le.clear()
            # self.pwd_le.setText("")
            self.account_le.setFocus()
            return None
        if (status == AccountTool.PWD_ERROR):
            print("密码错误")
            self.pwd_le.clear()
            self.pwd_le.setFocus()
            return None
        else:
            print("登陆成功")








    def resizeEvent(self, evt):
        widget_width = 150
        widget_height = 40
        margin = 60
        self.account_le.resize(widget_width, widget_height)
        self.pwd_le.resize(widget_width, widget_height)
        self.login_btn.resize(widget_width, widget_height)

        x = (self.width() - widget_width) // 2
        y = self.height() // 5
        self.account_le.move(x, y)
        self.pwd_le.move(x, self.account_le.y() + margin)
        self.login_btn.move(x, self.pwd_le.y() + margin)

        

# 1、创建一个应用程序对象
app = QApplication(sys.argv)

# 2、控件操作
# 创建控件、设置控件、事件和信号的处理
# 2.1 创建控件
window = Window()
# 2.2 设置控件


# 2.3 展示控件
window.show()

# 3、应用程序的执行，进入到消息循环
sys.exit(app.exec_())